package com.begye.smartloan.apptest.renzheng.adapter

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.begye.smartloan.apptest.R
import com.begye.smartloan.apptest.ext.isUrl
import org.json.JSONObject

class FillItemBangKaSubmitDialogAdapter(var dataList: List<JSONObject>) :
    RecyclerView.Adapter<FillItemBangKaSubmitDialogAdapter.ViewHolder>() {

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int,
    ): ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.list_item_fill_item_bang_ka_submit_dialog, parent, false)
        return ViewHolder(view)
    }

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        private val listItemBangKaSubmitDialogIcon: ImageView =
            view.findViewById(R.id.listItemBangKaSubmitDialogIcon)
        private val listItemBangKaSubmitDialogChannel: TextView =
            view.findViewById(R.id.listItemBangKaSubmitDialogChannel)
        private val listItemBangKaSubmitDialogMobile: TextView =
            view.findViewById(R.id.listItemBangKaSubmitDialogMobile)

        fun bindData(position: Int, item: JSONObject) {
            listItemBangKaSubmitDialogChannel.text = item.optString("walletDesc", "")
            listItemBangKaSubmitDialogMobile.text = item.optString("walletMobile", "")

            val logo = item.optString("walletLogo", "")
            if (logo.isUrl()) {
                Glide.with(listItemBangKaSubmitDialogIcon).load(logo)
                    .into(listItemBangKaSubmitDialogIcon)
            }
        }
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bindData(position, dataList[position])
    }

    override fun getItemCount(): Int {
        return dataList.size
    }
}